package com.study.service.impl;


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.study.configs.RedisKeys;
import com.study.dao.DangjMapper;

import com.study.pojo.entity.StudyGroup;
import com.study.pojo.resp.BaserResp;
import com.study.service.DangjService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;


/**
 * 中心学习组数
 */
@Service
public class DangjServiceImpl extends ServiceImpl<DangjMapper, StudyGroup> implements DangjService {
    @Autowired
    DangjMapper dangjMapper;
    @Autowired
    RedisTemplate redisTemplate;

    /**
     * 查询所有中心学习内容
     * @return
     */
    @Override
    public BaserResp findAllStudyGroup(Integer studyClassify) {
        //判断redis中是否有学习内容,直接判断大key;
        Boolean aBoolean = redisTemplate.hasKey(RedisKeys.STUDY_GROUP+studyClassify.toString());
        if(!aBoolean){
            //如果没有需要从数据库里查询
            List<StudyGroup> allByStauts = dangjMapper.findAllByStauts(studyClassify);

//            List<StudyGroup> studyGroups = dangjMapper.selectList(new LambdaQueryWrapper<StudyGroup>().eq(StudyGroup::getStudyClassify, studyClassify));
//            System.out.println(studyGroups);
            if(allByStauts!=null){
                //遍历后存入redis中  key1:中心组学习分类id  key2:学习内容id  value: 学习内容
                for (StudyGroup s:allByStauts
                     ) {
                    redisTemplate.opsForHash().put(RedisKeys.STUDY_GROUP+studyClassify,s.getStudyId().toString(),s);
                }
            }else {
                // TODO 如果为空,防止内穿透
                redisTemplate.opsForHash().put(RedisKeys.STUDY_GROUP,studyClassify,"1");
                //设置失效时间
                redisTemplate.expire(RedisKeys.STUDY_GROUP,60, TimeUnit.SECONDS);
            }
        }
        //类型id获取对应内容
        Map entries = redisTemplate.opsForHash().entries(RedisKeys.STUDY_GROUP + studyClassify.toString());
        if(StringUtils.isEmpty(entries.toString())){
            return new BaserResp().ERROR();
        }else {
            return new BaserResp(1,"ok",entries,null);
        }

    }


    /**
     * 通过分类id和信息id从redis中取出
     * @param studyClassify
     * @param studyId
     * @return
     */
    @Override
    public BaserResp selectOne(Integer studyClassify, Integer studyId){
        Object o = redisTemplate.opsForHash().get(RedisKeys.STUDY_GROUP + studyClassify.toString(), studyId.toString());
            if(StringUtils.isEmpty(o.toString())){
                return new BaserResp().ERROR();
            }else {
                return new BaserResp(1,"ok",o,null);
            }

    }

    /**
     * 添加投诉信息
     * @param
     * @return
     */
    @Override
    public BaserResp insert(String rbTitle,String rbContext){
        Integer count = dangjMapper.insertbox(rbTitle,rbContext);
        if(count==1){
            return new BaserResp(1,"提交成功",null,null);
        }else{
            return new BaserResp().ERROR();
        }

    }

    /**
     * 从redis中查到数据和
     * @param studyId
     * @return
     */
    @Override
    public BaserResp findStudyGroupById(Integer studyId) {
        Object o = redisTemplate.opsForHash().get(RedisKeys.STUDY_GROUP , studyId.toString());
            if(StringUtils.isEmpty(o.toString())){
                return new BaserResp().ERROR();
            }else {
                return new BaserResp(1,"ok",o,null);
            }
    }


}
